package com.vrv.netmgr.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vrv.common.utils.MD5;
import com.vrv.common.utils.StringUtil;
import com.vrv.framework.dao.BaseDAO;
import com.vrv.framework.service.EntityServiceImpl;
import com.vrv.framework.service.LogicProcessResult;
import com.vrv.netmgr.dao.ClassesDAO;
import com.vrv.netmgr.dao.PermissionGroupDAO;
import com.vrv.netmgr.dao.UserDAO;
import com.vrv.netmgr.entity.Classes;
import com.vrv.netmgr.entity.PermissionGroup;
import com.vrv.netmgr.entity.User;
import com.vrv.netmgr.vo.UserSearchCondition;

/**
 * User service实现类
 *
 * @author 曹保利
 * @version 1.0
 *
 */
@Service(value = "userService")
public class UserServiceImpl extends EntityServiceImpl<User> implements UserService {

    @Autowired
    private UserDAO userDao;
    
    @Autowired
    private ClassesDAO classesDao;

    @Autowired
    private PermissionGroupDAO permissionGroupDao;
    
    @Autowired
    @Qualifier("userDao")
    public void setUserDao(BaseDAO<User, Serializable> userDao) {
        super.dao = userDao;
    }

    @Override
    public User getUserByLoginId(UserSearchCondition userSearchCondition) {
        String passWord = userSearchCondition.getPassWord();
        if (!StringUtil.isValidString(passWord)) {
            return null;
        }

        List<User> users = userDao.findUserByLoginId(userSearchCondition.getName());
        for (User user : users) {
            if (MD5.GetMD5Code(passWord).toUpperCase().equals(user.getPassWord())) {
                List<PermissionGroup> groups = getPermissionGroupByUser(user);
                List<Classes> classes = getClassesByUser(user);
                user.setGroups(groups);
                user.setClasses(classes);
                return user;
            }
        }
        return null;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public LogicProcessResult save(User valueObject) {
        // 处理加密
        String passWord = valueObject.getPassWord();
        passWord = MD5.GetMD5Code(passWord).toUpperCase();
        valueObject.setPassWord(passWord);
        // 继续父类的操作
        return super.save(valueObject);
    }

    @Override
    public List<PermissionGroup> getPermissionGroupByUser(User user) {
        String[] ids = null;
        if (StringUtil.isValidString(user.getGroupIds()) && user.getGroupIds().length() > 0) {
            ids = user.getGroupIds().split(";");
        }
        if (ids == null) {
            return null;
        } else {
            return permissionGroupDao.findGroupsByUserIds(ids);
        }
    }

    /* (non-Javadoc)
     * @see com.vrv.netmgr.service.UserService#getClassesByUser(com.vrv.netmgr.entity.User)
     */
    @Override
    public List<Classes> getClassesByUser(User user) {
         String[] ids = null;
         if (StringUtil.isValidString(user.getClassIds()) && user.getClassIds().length() > 0) {
             ids = user.getClassIds().split(";");
         }
         if (ids == null) {
             return null;
         } else {
             return classesDao.findClassesByUserIds(ids);
         }
    }

}
